<?php
namespace app\index\controller;

use app\common\controller\HomeBase;
use org\Page;
use think\Db;
use think\facade\Config;
use think\facade\Env;

class Category extends HomeBase
{
    public function index()
    {
        $page = (int)$this->request->param("page",1);

        $page_size = 10;
        $offset = ($page - 1) * $page_size;

        $top_id = (int)$this->request->param("top_id");
        $sub_id = (int)$this->request->param("sub_id");

        $all_top_category = model("category")->where(['pid' => 0])->all();
        $all_sub_category = model("category")->where(['pid' => $top_id])->all();

        $model = model('article')->where(['cid' => $sub_id])->order('sort desc');
        $category = model('article')->where(['id' => $sub_id])->find();

        $total = $model->count();

        $article_list = $model->limit($offset,$page_size)->select();

        $pageObj = new Page(sprintf("c-%d-%d-{PAGE}",$top_id,$sub_id), $page, $total, $page_size);

        if (isMobileDomain()) {
            $pageObj->setConfig("theme",'%NO_PREV% %UP_PAGE% %DOWN_PAGE% %HEADER%');
        }

        $show = $pageObj->showPc();
        $this->assign("pages",$show);


        $this->assign("all_top_category",$all_top_category);
        $this->assign("all_sub_category",$all_sub_category);
        $this->assign("article_list",$article_list);
        $this->assign("top_id",$top_id);
        $this->assign("sub_id",$sub_id);
        $this->assign("total",$total);

        $this->site_seo("classify",['category_name' => $category['title']]);

        return $this->fetch();
    }


    function search()
    {
        $word = $this->request->param("word");
        $page = (int)$this->request->param("page",1);

        $page_size = 15;
        $offset = ($page - 1) * $page_size;

        if (empty($word))
        {
            return $this->error("搜索关键字不能为空");
        }

        $fielPath = Env::get('app_path') . "config/yuanwenku.ini";
        $xs =  new \XS($fielPath);
        $index = $xs->getIndex();
        $search = $xs->getSearch();
        $search = $search->setLimit($page_size,$offset)->setQuery($word);

        // 预计搜索结果总数
        $total = $search->count();
        $docs = $search->search();

        // 分页
        $pageObj = new Page(sprintf("search-%s-{PAGE}",$word), $page, $total, $page_size);

        if (isMobileDomain()) {
            $pageObj->setConfig("theme",'%NO_PREV% %UP_PAGE% %DOWN_PAGE% %HEADER%');
        }
        $show = $pageObj->showPc();
        $this->assign("pages",$show);

        $list = [];
        foreach ($docs as $key => $item)
        {
            if ($item['uid'] != 0)
            {
                $temp = model('article')->where(['id' => $item['uid']])->find();
                if (empty($temp)) {
                    $index->del($item['uid']);
                }else {
                    $list[$key] = $temp;
                    $words = explode(" ",$word);
                    if (is_array($words))
                    {
                       foreach ($words as $word)
                       {
                           $word = trim($word);
                           $list[$key]['content'] = str_replace($word,"<span style='color: red'>".$word."</span>",$item['content']);
                           $list[$key]['title'] = str_replace($word,"<span style='color: red'>".$word."</span>",$list[$key]['title']);
                       }
                    }else {
                       $list[$key]['content'] = str_replace($word,"<span style='color: red'>".$word."</span>",$item['content']);
                       $list[$key]['title'] = str_replace($word,"<span style='color: red'>".$word."</span>",$list[$key]['title']);
                    }
                }

            }
        }

        $this->assign("list",$list);
        $this->assign("total",$total);
        $this->site_seo("search",['word' => $word]);
        return $this->fetch();
    }
}
